package com.web.servlet;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.web.domain.CartItem;
import com.web.domain.User;
import com.web.service.BaseService;
import com.web.service.CartService;
import com.web.service.ProductService;
import com.web.utils.BeanFactory;

/**
 * Servlet implementation class CartServlet
 */
@WebServlet("/cart") 
public class CartServlet extends BaseService {
	private static final long serialVersionUID = 1L;
	private String activeUser;
	
	
	CartService cs = (CartService) BeanFactory.getBean("CartService");

	public String addToCart(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			request.setCharacterEncoding("UTF-8");

			/*
			 * 接收pid & count
			 */
			int count = Integer.parseInt(request.getParameter("count"));
			String pid = request.getParameter("pid");
			
			/*
			 * 判断是否已经登陆
			 */
				User user = (User) request.getSession().getAttribute("user");
				activeUser = user.getUid();
			
			/*
			 * 查找数据库是否有相同数据
			 */
			cs.getDateCount(activeUser, pid,count);
			
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return getData(request, response);
	}


	/*
	 * 显示Cart
	 */


	public String getData(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			// 获取Product对象
			ProductService ps = (ProductService) BeanFactory.getBean("ProductService");

			// 将数据库数据取出
			if(request.getSession().getAttribute("user")!=null) {
				User user = (User) request.getSession().getAttribute("user");
				activeUser = user.getUid();
			}else {
				//response.sendRedirect(request.getContextPath()+"/UserService?method=loginUI");
				return "/UserService?method=loginUI";
			}
			List<CartItem> cartF = cs.getFromCart(activeUser);
			double total = 0.0;
			for (CartItem cartItem2 : cartF) {
				String productId = cartItem2.getCt_pid();
				cartItem2.setProduct(ps.getById(productId));
				total += cartItem2.getSubtotal();
			}
			request.setAttribute("total", total);
			request.setAttribute("cart", cartF);
		} catch (Exception e) {
			request.setAttribute("msg", "获取商品信息失败！程序猿在紧急处理中……");
			return "/jsp/msg.jsp";
		}
		return "/jsp/cart.jsp";
	}
	
	/*
	 * 通过id删除单个商品
	 */
	public String deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接收ct_id
		try {
			String ct_id = request.getParameter("ct_id");
			cs.deleteById(ct_id);
			cs.getDataSub(ct_id);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			request.setAttribute("msg", "删除失败！您还是买了吧！！");
			return "/jsp/msg.jsp";
		}
		return getData(request, response);
	}
	
	/*
	 * 清空购物车
	 */
	public String deleteAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			String ct_uid = request.getParameter("ct_uid");
			cs.deleteAll(ct_uid);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			request.setAttribute("msg", "删除失败！您还是买了吧！！");
			return "/jsp/msg.jsp";
		}
		return "/jsp/cart.jsp";
	}
}










